package com.event.content;

import java.io.IOException;
import java.lang.reflect.Field;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import com.example.logindesign.R;
import com.spinner.keyvalue.SpinnerClass;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TaskStackBuilder;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewConfiguration;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.Toast;

public class EventEditActivity extends Activity {
	private static final String NAMESPACE = "http://tempuri.org/";
	private static final String ENDPOINT = "192.168.1.198:1536/CommunitySystemsicWsWF.asmx";
	private String methodName;
	private String soapAction;
	private String unitcode;
	private String username;
	private String userid;
	private String eventID;
	// 4个下拉列表初始值
	private String sjlxInitValue, sjlbInitValue, clfsInitValue,
			clfsToInitValue;
	// 编辑添加标识
	private String operateFlag = "Add";
	// 保存按钮
	private Button btnSave;
	// 返回 个按钮
	private ImageButton btnBack;
	// 分流上报隐藏的tableRowHidden
	private TableRow tableRowHidden;
	// 四个spinner控件
	private Spinner spinnerSjlx, spinnerSjlb, spinnerClfs, spinnerClfsTo;
	private ArrayList<SpinnerClass> spinnerSjlxArrayList;
	private ArrayAdapter<SpinnerClass> spinnerSjlxAdapter;
	private ArrayList<SpinnerClass> spinnerSjlbArrayList;
	private ArrayAdapter<SpinnerClass> spinnerSjlbAdapter;
	private ArrayList<SpinnerClass> spinnerClfsArrayList;
	private ArrayAdapter<SpinnerClass> spinnerClfsAdapter;
	private ArrayList<SpinnerClass> spinnerClfsToArrayList;
	private ArrayAdapter<SpinnerClass> spinnerClfsToAdapter;

	// textview控件 12个
	private EditText editTextSjrs;
	private EditText editTextFxcs;
	private EditText editTextDsr;
	private EditText editTextLxfs;
	private EditText editTextSjms;
	private EditText editTextDjr;
	private EditText editTextDjsj;
	private EditText editTextLrr;
	private EditText editTextLrjg;
	private EditText editTextSswg;
	private EditText editTextClr;
	private EditText editTextClyj;

	// EditText的值
	private String editTextSjrsValue = "";
	private String editTextFxcsValue = "";
	private String editTextDsrValue = "";
	private String editTextLxfsValue = "";
	private String editTextSjmsValue = "";
	private String editTextDjrValue = "";
	private String editTextDjsjValue = "";
	private String editTextLrrValue = "";
	private String editTextLrjgValue = "";
	private String editTextSswgValue = "";
	private String editTextClrValue = "";
	private String editTextClyjValue = "";
	// 4个spinner
	private String spinnerSjlxSave = "";
	private String spinnerSjlbSave = "";
	private String spinnerClfsSave = "";
	private String spinnerClfsToSave = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_event_edit);
		// sharedpreference中取得相应值
		getValueFromSharedPreferences();
		// 绑定EditText等控件
		initEditTextEtc();
		// 设置下拉框绑定及监听
		initSpinner();
		// 保存、返回按钮绑定及监听
		initButton();

	}

	/**
	 * sharedpreference中取得相应值
	 */
	private void getValueFromSharedPreferences() {
		// TODO Auto-generated method stub
		SharedPreferences sharedPreferences = getSharedPreferences("UserInfo",
				Activity.MODE_PRIVATE);
		username = sharedPreferences.getString("username", "");
		userid = sharedPreferences.getString("userid", "");
		unitcode = sharedPreferences.getString("unitcode", "");
	}

	/**
	 * 保存按钮绑定及监听
	 */
	private void initButton() {
		// TODO Auto-generated method stub
		btnBack = (ImageButton) findViewById(R.id.btn_event_edit_back);
		btnBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 返回listview页面
				skipListViewActivity();
			}
		});
		btnSave = (Button) findViewById(R.id.btn_event_edit_save);
		btnSave.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				editTextSjrsValue = editTextSjrs.getText().toString().trim();
				editTextFxcsValue = editTextFxcs.getText().toString().trim();
				editTextDsrValue = editTextDsr.getText().toString().trim();
				;
				editTextLxfsValue = editTextLxfs.getText().toString().trim();
				;
				editTextSjmsValue = editTextSjms.getText().toString().trim();
				;
				editTextDjrValue = editTextDjr.getText().toString().trim();
				;
				editTextDjsjValue = editTextDjsj.getText().toString().trim();
				;
				editTextLrrValue = editTextLrr.getText().toString().trim();
				;
				editTextLrjgValue = editTextLrjg.getText().toString().trim();
				;
				editTextSswgValue = editTextSswg.getText().toString().trim();
				;
				editTextClrValue = editTextClr.getText().toString().trim();
				;
				editTextClyjValue = editTextClyj.getText().toString().trim();
				spinnerSjlxSave = spinnerSjlx.getSelectedItem().toString();
				spinnerSjlbSave = spinnerSjlb.getSelectedItem().toString();
				spinnerClfsSave = spinnerClfs.getSelectedItem().toString();
				// 提交数据
				new saveDataTask().execute();
			}
		});
	}

	private class saveDataTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			String resultString = submitData();
			try {
				JSONArray jsonArray = new JSONArray(resultString);
				JSONObject jsonObject = jsonArray.optJSONObject(0);
				if (jsonObject.optString("Code").equals("1")) {
					return "success";
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "fail";
			}
			return "fail";
		}

		protected void onPostExecute(String finallyResultString) {
			if (finallyResultString.equals("success")) {
				Toast.makeText(EventEditActivity.this, operateFlag + "成功",
						Toast.LENGTH_LONG).show();
				// 跳转到listview页面
				skipListViewActivity();
			} else {
				Toast.makeText(EventEditActivity.this, operateFlag + "失败",
						Toast.LENGTH_LONG).show();
			}

		}
	}

	/**
	 * 提交数据
	 * 
	 * @return
	 */
	private String submitData() {
		// TODO Auto-generated method stub
		String resultString = "";
		if (operateFlag.equals("Edit")) {
			methodName = "modify_SysGridMyEvent";
			soapAction = "http://tempuri.org/modify_SysGridMyEvent";
		} else {
			methodName = "add_SysGridMyEvent";
			soapAction = "http://tempuri.org/add_SysGridMyEvent";
		}
		SoapObject soapObject = new SoapObject(NAMESPACE, methodName);
		// 当前提交时间 yyyy-MM-dd HH:mm:ss
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		Date curDate = new Date(System.currentTimeMillis());
		if (operateFlag.equals("Edit")) {
			// 操作人员id
			soapObject.addProperty("userid", userid);
		}
		// 操作人员所属区域编码
		soapObject.addProperty("unitcode", unitcode);
		if (operateFlag.equals("Edit")) {
			// 事件管理id
			soapObject.addProperty("id", eventID);
		}
		// 事件类型
		soapObject.addProperty("shijianleixing", spinnerSjlxSave);
		// 事件类别
		soapObject.addProperty("shijianleibie", spinnerSjlbSave);
		// 涉及人数
		soapObject.addProperty("shejirenshu", editTextSjrsValue);
		// 联系方式
		soapObject.addProperty("lianxifangshi", editTextLxfsValue);
		// 发现场所
		soapObject.addProperty("faxianchangsuo", editTextFxcsValue);
		// 当事人
		soapObject.addProperty("dangshiren", editTextDsrValue);
		// 事件描述
		soapObject.addProperty("shijianmiaoshu", editTextSjmsValue);
		// 登记人
		soapObject.addProperty("dengjiren", editTextDjrValue);
		// 登记时间
		soapObject.addProperty("dengjishijian", editTextDjsjValue);
		// 录入人
		soapObject.addProperty("lururen", editTextLrrValue);
		// 录入机构
		soapObject.addProperty("lurujigou", editTextLrjgValue);
		// 所属网格
		soapObject.addProperty("suoshuwangge", editTextSswgValue);
		// 处理意见
		soapObject.addProperty("chuliyijian", editTextClyjValue);
		// 处理方式
		soapObject.addProperty("chulifangshi", spinnerClfsSave);
		// 选择的 转办至、分流至 人员
		soapObject.addProperty("fenliuzhi", spinnerClfsToSave);
		if (operateFlag.equals("Edit")) {
			Log.e("editer", operateFlag);
			// 当前操作人员用户名
			soapObject.addProperty("editer", username);
			// 当前提交时间 yyyy-MM-dd HH:mm:ss
			soapObject.addProperty("edittime", curDate.toString());
		} else {
			// 当前操作人员用户名
			soapObject.addProperty("inputer", username);
			// 当前提交时间 yyyy-MM-dd HH:mm:ss
			soapObject.addProperty("inputtime", curDate.toString());
		}

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(soapObject);
		Log.e("soapobject", soapObject.toString());
		envelope.dotNet = true;
		HttpTransportSE transportSE = new HttpTransportSE(ENDPOINT);
		try {
			transportSE.call(soapAction, envelope);
			if (envelope.getResponse() != null) {
				SoapObject object = (SoapObject) envelope.bodyIn;
				resultString = object.getProperty(0).toString();
				Log.e("webservice取得", resultString);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("exception了二楼", e.getMessage());
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resultString;
	}


	/**
	 * 设置EditText等控件，不能有耗时的操作
	 */
	private void initEditTextEtc() {
		// TODO Auto-generated method stub
		// findviewbyid
		tableRowHidden = (TableRow) findViewById(R.id.tableRowHidden);
		editTextSjrs = (EditText) findViewById(R.id.EditTextSjrs);
		editTextFxcs = (EditText) findViewById(R.id.EditTextFxcs);
		editTextDsr = (EditText) findViewById(R.id.EditTextDsr);
		editTextLxfs = (EditText) findViewById(R.id.EditTextLxfs);
		editTextSjms = (EditText) findViewById(R.id.EditTextSjms);
		editTextDjr = (EditText) findViewById(R.id.EditTextDjr);
		editTextDjsj = (EditText) findViewById(R.id.EditTextDjsj);
		editTextLrr = (EditText) findViewById(R.id.EditTextLrr);
		editTextLrjg = (EditText) findViewById(R.id.EditTextLrjg);
		editTextSswg = (EditText) findViewById(R.id.EditTextSswg);
		editTextClr = (EditText) findViewById(R.id.EditTextClr);
		editTextClyj = (EditText) findViewById(R.id.EditTextClyj);
		// setText 更新ui耗时操作放到了线程里
		new setEditTextEtcTask().execute();
	}

	/**
	 * 登记时间点击弹出日期时间选择器
	 * 
	 * @param v
	 */
	public void txtDjsjClick(View v) {
		AlertDialog.Builder builder = new AlertDialog.Builder(
				EventEditActivity.this);
		View view = View.inflate(this, R.layout.date_time_dialog, null);
		final DatePicker datePicker = (DatePicker) view
				.findViewById(R.id.datePicker);
		final TimePicker timePicker = (TimePicker) view
				.findViewById(R.id.timePicker);
		builder.setView(view);
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(System.currentTimeMillis());
		datePicker.init(calendar.get(calendar.YEAR),
				calendar.get(calendar.MONTH),
				calendar.get(calendar.DAY_OF_MONTH),
				new OnDateChangedListener() {

					@Override
					public void onDateChanged(DatePicker view, int year,
							int monthOfYear, int dayOfMonth) {
						// TODO Auto-generated method stub
						// 执行线程
						// chooseTime.setText("您选择的日期是:" + year
						// + "年" + monthOfYear + 1 + "月" + dayOfMonth
						// + "日");
						// new AlertDialogChooseTime().execute("您选择的日期是:" + year
						// + "年" + monthOfYear + 1 + "月" + dayOfMonth
						// + "日");
					}
				});
		timePicker.setIs24HourView(true);
		timePicker.setCurrentHour(calendar.get(calendar.HOUR_OF_DAY));
		timePicker.setCurrentMinute(calendar.get(calendar.MINUTE));
		timePicker.setOnTimeChangedListener(new OnTimeChangedListener() {

			public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
				// TODO Auto-generated method stub
				// chooseTime.setText(chooseTime.getText()+" " + hourOfDay + "时"
				// + minute + "分");
				// new AlertDialogChooseTime().execute(" " + hourOfDay + "时"
				// + minute + "分");
			}
		});

		builder.setTitle("选择登记时间");
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				StringBuffer stringBuffer = new StringBuffer();
				stringBuffer.append(String.format("%d-%02d-%02d %02d:%02d",
						datePicker.getYear(), datePicker.getMonth() + 1,
						datePicker.getDayOfMonth(),
						timePicker.getCurrentHour(),
						timePicker.getCurrentMinute()));
				editTextDjsj.setText(stringBuffer);
				dialog.dismiss();
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		Dialog dialog = builder.create();
		dialog.show();
	}

	/**
	 * 设置下拉框绑定及监听
	 */
	private void initSpinner() {
		// TODO Auto-generated method stub
		spinnerSjlx = (Spinner) findViewById(R.id.spinnerSjlx);
		spinnerSjlb = (Spinner) findViewById(R.id.spinnerSjlb);
		spinnerClfs = (Spinner) findViewById(R.id.spinnerClfs);
		spinnerClfsTo = (Spinner) findViewById(R.id.spinnerClfsTo);
		
		// 绑定适配器和值 放到线程里
		spinnerSjlxArrayList = new ArrayList<SpinnerClass>();
		spinnerSjlxArrayList.add(new SpinnerClass("请选择", ""));
		spinnerSjlxAdapter = new ArrayAdapter<SpinnerClass>(
				EventEditActivity.this,
				android.R.layout.simple_spinner_dropdown_item,
				spinnerSjlxArrayList);
		spinnerSjlx.setAdapter(spinnerSjlxAdapter);
		spinnerSjlxAdapter.notifyDataSetChanged(); // 通知spinner刷新数据

		spinnerSjlbArrayList = new ArrayList<SpinnerClass>();
		spinnerSjlbArrayList.add(new SpinnerClass("请选择", ""));
		spinnerSjlbAdapter = new ArrayAdapter<SpinnerClass>(
				EventEditActivity.this,
				android.R.layout.simple_spinner_dropdown_item,
				spinnerSjlbArrayList);
		spinnerSjlb.setAdapter(spinnerSjlbAdapter);
		spinnerSjlbAdapter.notifyDataSetChanged();

		spinnerClfsArrayList = new ArrayList<SpinnerClass>();
		spinnerClfsArrayList.add(new SpinnerClass("请选择", ""));
		spinnerClfsAdapter = new ArrayAdapter<SpinnerClass>(
				EventEditActivity.this,
				android.R.layout.simple_spinner_dropdown_item,
				spinnerClfsArrayList);
		spinnerClfs.setAdapter(spinnerClfsAdapter);
		spinnerClfsAdapter.notifyDataSetChanged();

		spinnerClfsToArrayList = new ArrayList<SpinnerClass>();
		spinnerClfsToArrayList.add(new SpinnerClass("请选择", ""));
		spinnerClfsToAdapter = new ArrayAdapter<SpinnerClass>(
				EventEditActivity.this,
				android.R.layout.simple_spinner_dropdown_item,
				spinnerClfsToArrayList);
		spinnerClfsTo.setAdapter(spinnerClfsToAdapter);
		spinnerClfsToAdapter.notifyDataSetChanged();
		
		//设置事件类型、处理方式下拉数据
		new setSjlxSpinnerTask().execute();
		new setClfsSpinnerTask().execute();
		
		// 设置监听事件
		spinnerSjlx.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				// position为当前选中的值的序号
				// 将事件类别适配器的值改变为spinner[position]中的值
				// view
				// 是你点击的Listview的某一项的内容，来源于adapter。如用((TextView)arg1).getText()，可以取出点击的这一项的内容，转为string类型。
				// postion是adapter的某一项，如点击了listview第2项，而第2项对应的是adapter的第2个数值，那此时position的值就为1了。
				// id的值为点击了Listview的哪一项对应的数值，点击了listview第2项，那id就等于1。
				// spinnerSjlbAdapter = new
				// ArrayAdapter<>(EventEditActivity.this,
				// android.R.layout.simple_spinner_dropdown_item,
				// spinnerSjlbValue[position]);
				// spinnerSjlb.setAdapter(spinnerSjlbAdapter);
				// spinnerSjlbAdapter.notifyDataSetChanged();
				// Toast.makeText(EventEditActivity.this, "假的",
				// Toast.LENGTH_LONG)
				// .show();
				new setSjlbSpinnerTask().execute();
			}
			
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
		// 设置监听事件
		spinnerClfs.setOnItemSelectedListener(new OnItemSelectedListener() {
			
			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				String selectedValue = ((SpinnerClass) spinnerClfs
						.getSelectedItem()).getValue();
				if (selectedValue.equals("上报") || selectedValue.equals("分流")) {
					tableRowHidden.setVisibility(View.VISIBLE);
					new setClfsToSpinnerTask().execute();
				} else {
					tableRowHidden.setVisibility(View.GONE);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	/**
	 * 填充处理方式to下拉数据
	 */
	private class setClfsToSpinnerTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			String resultString = getSpinnerData("clfsTo");
			String finallyResultString = jsonFormatSpinner(resultString,
					"clfsTo");
			return finallyResultString;
		}

		protected void onPostExecute(String finallyResultString) {
			if (finallyResultString.equals("success")) {
				// 初始化同时删除其他选项，只能显示自己和下级的数据。
				int len = spinnerClfsToArrayList.size();
				for (int i = 0; i < len; i++) {
					SpinnerClass SpinnerClass = spinnerClfsToArrayList.get(i);
					if (SpinnerClass.getValue().equals(clfsToInitValue)) {
						spinnerClfsTo.setSelection(i, true);
						break;
					}
				}
				spinnerClfsToAdapter.notifyDataSetChanged();
			}
		}
	}

	/**
	 * 填充处理方式下拉数据
	 */
	private class setClfsSpinnerTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			String resultString = getSpinnerData("clfs");
			String finallyResultString = jsonFormatSpinner(resultString, "clfs");
			return finallyResultString;
		}

		protected void onPostExecute(String finallyResultString) {
			if (finallyResultString.equals("success")) {
				// 初始化同时删除其他选项，只能显示自己和下级的数据。
				int len = spinnerClfsArrayList.size();
				for (int i = 0; i < len; i++) {
					SpinnerClass SpinnerClass = spinnerClfsArrayList.get(i);
					if (SpinnerClass.getValue().equals(clfsInitValue)) {
						spinnerClfs.setSelection(i, true);
						break;
					}
				}
				spinnerClfsAdapter.notifyDataSetChanged();
			}
		}
	}

	/**
	 * 填充事件类别下拉数据
	 */
	private class setSjlbSpinnerTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			String resultString = getSpinnerData("sjlb");
			String finallyResultString = jsonFormatSpinner(resultString, "sjlb");
			return finallyResultString;
		}

		protected void onPostExecute(String finallyResultString) {
			if (finallyResultString.equals("success")) {
				// 初始化同时删除其他选项，只能显示自己和下级的数据。
				int len = spinnerSjlbArrayList.size();
				for (int i = 0; i < len; i++) {
					SpinnerClass SpinnerClass = spinnerSjlbArrayList.get(i);
					if (SpinnerClass.getValue().equals(sjlbInitValue)) {
						spinnerSjlb.setSelection(i, true);
						break;
					}
				}
				spinnerSjlbAdapter.notifyDataSetChanged();
			}
		}
	}

	/**
	 * 填充事件类型下拉数据
	 */
	private class setSjlxSpinnerTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			String resultString = getSpinnerData("sjlx");
			String finallyResultString = jsonFormatSpinner(resultString, "sjlx");
			return finallyResultString;
		}

		protected void onPostExecute(String finallyResultString) {
			if (finallyResultString.equals("success")) {
				// 初始化
				int len = spinnerSjlxArrayList.size();
				for (int i = 0; i < len; i++) {
					SpinnerClass SpinnerClass = spinnerSjlxArrayList.get(i);
					if (SpinnerClass.getValue().equals(sjlxInitValue)) {
						spinnerSjlx.setSelection(i, true);
						break;
					}
				}
				spinnerSjlxAdapter.notifyDataSetChanged();
			}
		}
	}

	/**
	 * 登记时间等控件初始化
	 */
	private class setEditTextEtcTask extends AsyncTask<Void, Void, String> {

		protected void onPreExecute() {
			// 设置EditText值
			setEditTextValue();
		}

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			// SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
			// "yyyy-MM-dd HH:mm:ss");
			// Date curDate = new Date(System.currentTimeMillis());
			// return curDate.toString();
			Intent getEditorAdd = getIntent();
			operateFlag = getEditorAdd.getStringExtra("operateFlag");
			if (operateFlag.equals("Edit")) {
				eventID = getEditorAdd.getStringExtra("enentID");
				// 设置4个下拉列表的初始值
				sjlxInitValue = getEditorAdd.getStringExtra("shijianleixing");
				sjlbInitValue = getEditorAdd.getStringExtra("shijianleibie");
				clfsInitValue = getEditorAdd.getStringExtra("chulifangshi");
				clfsToInitValue = getEditorAdd.getStringExtra("chulifangshito");
				
				spinnerSjlxSave = getEditorAdd.getStringExtra("shijianleixing");
				spinnerSjlbSave = getEditorAdd.getStringExtra("shijianleibie");
				editTextSjrsValue = getEditorAdd.getStringExtra("shejirenshu");
				editTextLxfsValue = getEditorAdd
						.getStringExtra("lianxifangshi");
				editTextFxcsValue = getEditorAdd
						.getStringExtra("faxianchangsuo");
				editTextDsrValue = getEditorAdd.getStringExtra("dangshiren");
				editTextSjmsValue = getEditorAdd
						.getStringExtra("shijianmiaoshu");
				editTextDjrValue = getEditorAdd.getStringExtra("dengjiren");
				editTextDjsjValue = getEditorAdd
						.getStringExtra("dengjishijian");
				editTextSswgValue = getEditorAdd.getStringExtra("suoshuwangge");
				editTextLrrValue = getEditorAdd.getStringExtra("lururen");
				editTextLrjgValue = getEditorAdd.getStringExtra("lururenjigou");
				editTextClyjValue = getEditorAdd.getStringExtra("chuliyijian");
			}
			return null;
		}

		protected void onPostExecute(String resultString) {
			// 设置EditText值
			setEditTextValue();
		}
	}

	/**
	 * 设置EditText的值
	 */
	private void setEditTextValue() {
		// TODO Auto-generated method stub
		editTextDjsj.setText(editTextDjsjValue);
		editTextFxcs.setText(editTextFxcsValue);
		editTextDsr.setText(editTextDsrValue);
		editTextLxfs.setText(editTextLxfsValue);
		editTextSjms.setText(editTextSjmsValue);
		editTextDjr.setText(editTextDjrValue);
		editTextDjsj.setText(editTextDjsjValue);
		editTextLrr.setText(editTextLrrValue);
		editTextLrjg.setText(editTextLrjgValue);
		editTextSswg.setText(editTextSswgValue);
		editTextClr.setText(editTextClrValue);
		editTextClyj.setText(editTextClyjValue);
	}

	/**
	 * 连接webservice，得到字符串
	 * 
	 * @return
	 */
	private String getSpinnerData(String spinnername) {
		String resultString = "";
		SoapObject soapObject = null;
		if (spinnername.equals("sjlx")) {
			methodName = "get_sjlx";
			soapAction = "http://tempuri.org/get_sjlx";
			soapObject = new SoapObject(NAMESPACE, methodName);
		} else if (spinnername.equals("sjlb")) {
			methodName = "get_sjlb";
			soapAction = "http://tempuri.org/get_sjlb";
			soapObject = new SoapObject(NAMESPACE, methodName);
			soapObject.addProperty("sjlx",
					((SpinnerClass) spinnerSjlx.getSelectedItem()).getValue());
		} else if (spinnername.equals("clfs")) {
			methodName = "get_clfs";
			soapAction = "http://tempuri.org/get_clfs";
			soapObject = new SoapObject(NAMESPACE, methodName);
			soapObject.addProperty("username", username);
			soapObject.addProperty("unitcode", unitcode);
		} else if (spinnername.equals("clfsTo")) {
			methodName = "get_clfsAdmin";
			soapAction = "http://tempuri.org/get_clfsAdmin";
			soapObject = new SoapObject(NAMESPACE, methodName);
			soapObject.addProperty("username", username);
			soapObject.addProperty("state",
					((SpinnerClass) spinnerClfs.getSelectedItem()).getValue());
			soapObject.addProperty("unitcode", unitcode);
		}

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(soapObject);
		envelope.dotNet = true;
		HttpTransportSE transportSE = new HttpTransportSE(ENDPOINT);
		try {
			transportSE.call(soapAction, envelope);
			if (envelope.getResponse() != null) {
				SoapObject object = (SoapObject) envelope.bodyIn;
				resultString = object.getProperty(0).toString();
				Log.e("webservice取得", resultString);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resultString;
	}

	/**
	 * 解析json数据返回hashmap格式数据
	 * 
	 * @return
	 */
	private String jsonFormatSpinner(String resultString, String spinnername) {

		JSONArray jsonArray;
		try {
			jsonArray = new JSONArray(resultString);
			int len = jsonArray.length();
			JSONObject jsonObject = jsonArray.optJSONObject(0);

			if (jsonObject.optString("Code").equals("1")) {
				if (spinnername.equals("sjlx")) {
					spinnerSjlxArrayList.clear();
					for (int i = 1; i < len; i++) {
						// 根据实际从第2组开始取得有关数据
						jsonObject = jsonArray.optJSONObject(i);
						spinnerSjlxArrayList.add(new SpinnerClass(jsonObject
								.optString("key"), jsonObject
								.optString("value")));
					}
				} else if (spinnername.equals("sjlb")) {
					spinnerSjlbArrayList.clear();
					for (int i = 1; i < len; i++) {
						// 根据实际从第2组开始取得有关数据
						jsonObject = jsonArray.optJSONObject(i);
						spinnerSjlbArrayList.add(new SpinnerClass(jsonObject
								.optString("key"), jsonObject
								.optString("value")));
					}
				} else if (spinnername.equals("clfs")) {
					spinnerClfsArrayList.clear();
					for (int i = 1; i < len; i++) {
						// 根据实际从第2组开始取得有关数据
						jsonObject = jsonArray.optJSONObject(i);
						spinnerClfsArrayList.add(new SpinnerClass(jsonObject
								.optString("key"), jsonObject
								.optString("value")));
					}
				} else if (spinnername.equals("clfsTo")) {
					spinnerClfsToArrayList.clear();
					for (int i = 1; i < len; i++) {
						// 根据实际从第2组开始取得有关数据
						jsonObject = jsonArray.optJSONObject(i);
						spinnerClfsToArrayList.add(new SpinnerClass(jsonObject
								.optString("key"), jsonObject
								.optString("value")));
					}
				}
				return "success";
			} else {
				return "fail";
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "fail";
	}

	/**
	 * 跳转到listview页面
	 */
	private void skipListViewActivity() {
		Intent listviewIntent = new Intent();
		listviewIntent.setClass(EventEditActivity.this,
				EventListViewActivity.class);
		EventEditActivity.this.startActivity(listviewIntent);
		EventEditActivity.this.finish();
	}

	/**
	 * 重写keydown事件
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			skipListViewActivity();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
